<template>
  <div>
    <form role="form">
      <div class="form-group">
        <label>商品编号</label>
        <input type="text" class="form-control" v-model="gid" placeholder="请输入编号">
      </div>
      <div class="form-group">
        <label>名称</label>
        <input type="text" class="form-control" v-model="gname" placeholder="请输入名称">
      </div>
      <div class="form-group">
        <label>日期</label>
        <input type="date" class="form-control" v-model="gdate">
      </div>
      <div class="form-group">
        <label>类别</label>
        <select class="form-control" v-model="gcateid">
          <option v-for="c in courses" :key="c.cid" :value="c.cid">{{c.cname}}</option>
        </select>
      </div>
      <div class="form-group">
        <label>介绍</label>
        <textarea type="text" class="form-control" v-model="ginfo" placeholder="介绍"></textarea>
      </div>
      <button type="button" class="btn btn-primary" @click="add">添加</button>
      <button type="reset" class="btn btn-warning">重置</button>
    </form>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data(){
    return {
      gid:'',
      gname:'',
      gdate:'',
      gcateid:1,
      ginfo:'',
      courses:[]
    }
  },
  methods:{
    initcourse(){
      this.$ajax({
        url:'course',
        method:'get'
      }).then(resp=>{
        this.courses = resp.data.message
      })
    },
    add(){
      let obj = {
        gid:this.gid,
        gname:this.gname,
        gdate:moment(this.gdate).format('YYYY-MM-DD'),
        gcateid:this.gcateid,
        ginfo:this.ginfo
      }
      this.$ajax({
        url:'paper',
        method:'post',
        data:obj
      }).then(resp=>{
        this.$router.push('/')
      })
    }
  },
  created(){
    this.initcourse();
  }
}
</script>

<style scoped>

</style>